package com.burst.reptile.modular.app.controller;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.framework.config.WxMaConfiguration;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;

/**
 * 微信小程序 推送消息 服务器接收控制器
 */
@RestController
@RequestMapping("/wx/portal/{appid}")
public class WxPortalController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@GetMapping
	public void authGet(@PathVariable String appid,
			@RequestParam(name = "signature", required = false) String signature,
			@RequestParam(name = "timestamp", required = false) String timestamp,
			@RequestParam(name = "nonce", required = false) String nonce,
			@RequestParam(name = "echostr", required = false) String echostr, HttpServletResponse response){

		this.logger.info("\n接收到来自微信服务器的认证消息：signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
				signature, timestamp, nonce, echostr);

		if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
			throw new CustomException("请求参数非法，请核实!");
		}

		final WxMaService wxService = WxMaConfiguration.getMaService(appid);

		if (wxService.checkSignature(timestamp, nonce, signature)) {
			try {
				response.getWriter().print(echostr);
			} catch (IOException e) {
				throw new CustomException("IO异常");
			}
		} else {
			throw new CustomException("非法请求");
		}
	}

	@PostMapping(produces = "application/xml; charset=UTF-8")
	public void authPost(@PathVariable String appid, @RequestBody String requestBody,
			@RequestParam(name = "msg_signature", required = false) String msgSignature,
			@RequestParam(name = "encrypt_type", required = false) String encryptType,
			@RequestParam(name = "signature", required = false) String signature,
			@RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce,
			HttpServletResponse response) throws IOException {

		this.logger.info("\n接收微信请求：[msg_signature=[{}], encrypt_type=[{}], signature=[{}]," +
                " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
            msgSignature, encryptType, signature, timestamp, nonce, requestBody);

        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        final boolean isJson = Objects.equals(wxService.getWxMaConfig().getMsgDataFormat(),
            WxMaConstants.MsgDataFormat.JSON);
        if (StringUtils.isBlank(encryptType)) {
            // 明文传输的消息
            WxMaMessage inMessage;
            if (isJson) {
                inMessage = WxMaMessage.fromJson(requestBody);
            } else {//xml
                inMessage = WxMaMessage.fromXml(requestBody);
            }
            this.route(inMessage, appid);
            response.getWriter().print("success");
        }else if ("aes".equals(encryptType)) {
            // 是aes加密的消息
            WxMaMessage inMessage;
            if (isJson) {
                inMessage = WxMaMessage.fromEncryptedJson(requestBody, wxService.getWxMaConfig());
            } else {//xml
                inMessage = WxMaMessage.fromEncryptedXml(requestBody, wxService.getWxMaConfig(),
                    timestamp, nonce, msgSignature);
            }
            this.route(inMessage, appid);
            response.getWriter().print("success");
        }else {
        	throw new CustomException("不可识别的加密类型：" + encryptType);
        }
	}

	private void route(WxMaMessage message, String appid) {
		try {
			WxMaConfiguration.getRouter(appid).route(message);
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}
	}

}
